﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController
{

    int[][] indexArray;//1010  前两位行，后两位列 0 为空格子
    int[][] indexArrayCorrect;//校验是否结束的数组

    float useTime = 0f;
    public static bool pause = false;
    int step = 0;

    public void Init(int cow,int col)
    {
        pause = false;
        step = 0;
        useTime = 0;
        commandCache.Clear();
        indexArray = InitGridIndex(cow, col);
        indexArrayCorrect = InitGridIndex(cow, col);
        List<CommandType> rclst = CommandController.instance.GetRandomCommand(300);
        for (int i = 0; i < rclst.Count; i++)
        {
            ExcuteCommand(ref indexArray, rclst[i]);
        }
    }

    int[][] CloneArray(int[][] srcArray)
    {
        int[][] arrayTem = (int[][])srcArray.Clone();
        //Debug.LogError("arrayTem==null?"+ arrayTem==null);
        return arrayTem;
    }

    public int[][] GetCurGridIndex()
    {
        return indexArray;
    }


    public int[][] ExcuteCommand(ref int[][] indexArra,CommandType ct)
    {
        int rowLength = indexArra.Length;
        int colLength = indexArra[0].Length;
        int rowIndex=0;
        int colIndex=0;
        for (int i = 0; i < rowLength; i++)
        {
            for (int j = 0; j < rowLength; j++)
            {
                if (indexArra[i][j]==0)
                {
                    rowIndex = i;
                    colIndex = j;
                    break;
                }
            }
        }
        //Debug.LogError("rowLength:"+ rowLength);
        //Debug.LogError("colLength:" + colLength);
        //Debug.LogError("rowIndex:" + rowIndex);
        //Debug.LogError("colIndex:" + colIndex);
        switch (ct)
        {
            case CommandType.Up:
                if (rowIndex+1 <rowLength)
                {
                    //空格子往下走
                    indexArra[rowIndex][colIndex] = indexArra[rowIndex+1][colIndex];
                    indexArra[rowIndex+1][colIndex] = 0;//空格子重置为0
                }
                break;
            case CommandType.Down:
                if (rowIndex >0)
                {
                    //空格子往上走
                    indexArra[rowIndex][colIndex] = indexArra[rowIndex - 1][colIndex];
                    indexArra[rowIndex - 1][colIndex] = 0;//空格子重置为0
                }
                break;
            case CommandType.Left:
                if (colIndex + 1 < colLength)
                {
                    //空格子往左走
                    indexArra[rowIndex][colIndex] = indexArra[rowIndex][colIndex + 1];
                    indexArra[rowIndex][colIndex + 1] = 0;//空格子重置为0
                }
                break;
            case CommandType.Right:
                if (colIndex > 0)
                {
                    //空格子往右走
                    indexArra[rowIndex][colIndex] = indexArra[rowIndex][colIndex - 1];
                    indexArra[rowIndex][colIndex - 1] = 0;//空格子重置为0
                }
                break;
        }
        return indexArra;
    }

    public int[][] InitGridIndex(int row, int col)
    {
        int index = 1;
        int[][] array = new int[row][];
        for (int i = 0; i < row; i++)
        {
            array[i] = new int[col];
            for (int j = 0; j < col; j++)
            {
                array[i][j] = index;
                index++;
                if (i == row - 1 && j == col - 1)
                {
                    array[i][j] = 0;
                }
            }
        }
        return array;
    }

    public void Pause(bool state)
    {
        pause = state;
    }

    public bool CheckIsGameOver()
    {
        
        if (indexArray != null&& indexArrayCorrect!=null)
        {
            int row = indexArray.Length;
            int col = indexArray[0].Length;
            for (int r =0;r< row;r++)
            {
                for (int c = 0; c < col; c++)
                {
                    if (indexArray[r][c] != indexArrayCorrect[r][c])
                    {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public void ExcuteCommand(CommandType ct,bool isBack = false)
    {
        if (!isBack)
        {
            commandCache.Add(ct);
        } else
        {
            commandCache.RemoveAt(commandCache.Count-1);
        }
        
    }

    public void Back()
    {
        if (commandCache.Count > 0)
        {
            ExcuteCommand(commandCache[commandCache.Count-1]);
        }
    }

    public int GetCurUseTime()
    {
        return (int)useTime;
    }

    public int GetCurUseStep()
    {
        return step;
    }

    public void StepOne()
    {
        step++;
    }

    public void Update()
    {
        if (!pause)
        {
            useTime += Time.deltaTime;
        }
    }

    public void SaveCurGameProgress()
    {

    }

    public void ResumeLastGame()
    {

    }

    static GameController instance_;
    public static GameController instance
    {
        get
        {
            if (instance_ == null)
            {
                instance_ = new GameController();
            }
            return instance_;
        }
    }

    List<CommandType> commandCache = new List<CommandType>();
}



public enum GameMode
{
    Easy,
    Normal,
    Hard
}